package com.ruoyi.produce.process.scheduling.service;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.constant.ERPConstant;
import com.ruoyi.machine.base.device.domain.Device;
import com.ruoyi.machine.base.device.mapper.DeviceMapper;
import com.ruoyi.produce.base.shift.service.IProduceShiftInfoService;
import com.ruoyi.produce.base.shift.util.shiftInfoNoUtil;
import com.ruoyi.produce.process.scheduling.domain.Scheduling;
import com.ruoyi.produce.process.scheduling.mapper.SchedulingMapper;
import com.ruoyi.produce.process.worksheet.domain.ProduceWorkSheet;
import com.ruoyi.produce.process.worksheet.service.IProduceWorkSheetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@EnableScheduling
public class SchedulingServiceImpl implements ISchedulingService {
    @Autowired
    private SchedulingMapper schedulingMapper;
    @Autowired
    private IProduceShiftInfoService produceShiftInfoService;
    @Autowired
    private IProduceWorkSheetService workSheetService;
    @Autowired
    private DeviceMapper deviceMapper;


    /**
     * 加工单对应的排产查询
     * 需要传入加工单编号
     * 班次名称可选
     *
     * @param scheduling
     * @return
     */
    @Override
    public List<Scheduling> selectSchedulingList(Scheduling scheduling) {
        return schedulingMapper.selectSchedulingList(scheduling);
    }

    /**
     * 通过id查询排产记录
     * @param shiftInfoId
     * @param machineId
     * @return
     */
    @Override
    public List<Scheduling> selectSchedulingById(String shiftInfoId, String machineId) {
        return schedulingMapper.selectSchedulingById(shiftInfoId, machineId);
    }

    /**
     * 通过加工单编号查询有无排产记录
     * @param shiftInfoId
     * @param workSheetId
     * @return
     */
    @Override
    public Scheduling searchSchedulingByWorkSheetId(String shiftInfoId, String workSheetId) {
        return schedulingMapper.searchSchedulingByWorkSheetId(shiftInfoId, workSheetId);
    }

    /**
     * 通过加工单编号查询有无排产记录
     * @param workSheetId
     * @return
     */
    @Override
    public String searchSchedulingByWorkSheetId(String workSheetId) {
        Scheduling scheduling = schedulingMapper.searchSchedulingByWorkSheetId(produceShiftInfoService.createShiftInfoId(), workSheetId);
        if (StringUtils.isNotNull(scheduling)) {
            // 存在今天的排产
            return scheduling.getMachineId();
        }
        return " ";
    }

    /**
     * 根据加工单编号查询排产记录
     * @param workSheetId
     * @return
     */
    @Override
    public List<Scheduling> selectSchedulingByWorkSheetId(String workSheetId) {
        List<Scheduling> schedulingList = schedulingMapper.selectSchedulingByWorkSheetId(workSheetId);
        ProduceWorkSheet workSheet = workSheetService.selectProduceWorkSheetById(workSheetId);
        for (Scheduling scheduling : schedulingList) {
            scheduling.setProduceName(workSheet.getProduceName());
            if (!scheduling.getMachineId().equals("")) {
                Device device = deviceMapper.selectDeviceById(scheduling.getMachineId());
                scheduling.setMachineName(device.getMachineName());
            }
        }
        return schedulingList;
    }

    /**
     * 新增排产
     *
     * @param scheduling
     * @return
     */
    @Override
    public int insertScheduling(Scheduling scheduling) {
        return schedulingMapper.insertScheduling(scheduling);
    }

    /**
     * 删除排产
     *
     * @param scheduling
     * @return
     */
    @Override
    public int deleteScheduling(Scheduling scheduling) {
        return schedulingMapper.deleteScheduling(scheduling);
    }

    /**
     * 改变排产记录的完成状态
     * @param scheduling
     * @return
     */
    @Override
    public int updateCompletedStatus(Scheduling scheduling) {
        return schedulingMapper.updateCompletedStatus(scheduling);
    }

    /**
     * 在每天的19.00 5s，检查班次顺眼情况
     */
    @Scheduled(cron = "5 0 19 * * ?")
    public void postponedShift1() {
        String dayShiftId = produceShiftInfoService.createDayShiftId();// 生成当天的白班班次
        checkScheduling(dayShiftId);
    }

    /**
     * 在每天的7.00 5s，检查班次顺眼情况
     */
    @Scheduled(cron = "5 0 7 * * ?")
    public void postponedShift2() {
        String shiftInfoId = shiftInfoNoUtil.getNightId(); //昨天的夜班
        checkScheduling(shiftInfoId);
    }

    /**
     * 检查当前班次各加工单的排产情况，目的是如果到切班次的时候，可以自动顺延
     * @return
     */
    public void checkScheduling(String shiftId) {
        String shiftInfoId = produceShiftInfoService.createShiftInfoId(); // 生成当前时刻班次
        List<Scheduling> schedulingList = schedulingMapper.selectSchedulingByShiftInfoId(shiftId);// 查询上一个班次的排产记录
        for (Scheduling item : schedulingList) {
            ProduceWorkSheet workSheet = workSheetService.selectProduceWorkSheetById(item.getWorkSheetId());
            if (workSheet.getCompletionStatus().equals(ERPConstant.COMMON_UNFINISHED)) {
                // 未完成
                item.setShiftInfoId(shiftInfoId); //新的班次
                schedulingMapper.insertScheduling(item); //顺延
            }
        }
    }

}
